
import sys
import secrets
from enum import Enum
import logging
import logging.handlers
from typing import Optional

from pydantic.v1 import BaseSettings

class ExtendedEnum(Enum):
    @classmethod
    def list(cls):
        return list(map(lambda c: c.value, cls))

class Settings(BaseSettings):
    DESC: Optional[str] = """
    - 创新项目申报系统 
    - 实现：FastAPI
    """

    # SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
    SECRET_KEY: str = secrets.token_urlsafe(32)
    ALGORITHM = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES = 60*24*3 # 三天
    
    ORIGINS = [
        "http://localhost:5173",
        "https://127.0.0.1:5173",
        "http://localhost",
        "http://localhost:8080",
    ]
    
    # 部门参数
    class Department(str,ExtendedEnum):
        """
        部门
        """
        WLB = "网络部"
        LQ = "禄劝分公司"
        
    # 项目类别
    class ProjectClass(str,ExtendedEnum):
        PERSONAL = "个人项目"
        TEAM = "团队项目"
    
    # 项目类型    
    class ProjectType(str,ExtendedEnum):
        BIG = "大范围创新"
        SMALL = "小范围创新"
        MICRO = "微创新"
        
    # 项目状态
    class ProjectStatus(str,ExtendedEnum):
        DRAFT = "草稿" # 草稿
        SUBMITTED = "已提交" # 已提交
        PROFESSIONAL_AUDIT = "专家组会审" # 专家组会审
        PROFESSIONAL_AUDIT_FAILED = "专家组会审未通过" # 专家组会审未通过
        ROFESSIONAL_AUDIT_SUCCESS = "专家组会审通过" # 专家组会审通过
        INNOVATION_BOARD_AUDIT = "创新委员会会审" # 创新委员会会审
        INNOVATION_BOARD_AUDIT_SUCCESS = "创新委员会会审通过" # 创新委员会会审通过
        INNOVATION_BOARD_AUDIT_FAILED = "创新委员会会审未通过" # 创新委员会会审未通过

        CANCELED = "已作废"
    
    # 用户角色
    class UserRole(int,ExtendedEnum):
        Applicant = 1 # 申请者
        Auditor = 2 # 审核者
        Admin = 4 # 管理员
        
    # 项目评审组名称
    class ProjectGroupName(str,ExtendedEnum):
        WL = "网络专家组" # 网络专家组
        ZH = "综合专家组" # 综合专家组
        SC = "市场专家组" # 市场专家组
        GJ = "工建专家组" # 工建专家组
        JK = "集客专家组" # 集客专家组
        DY = "党业专家组" # 党业专家组
        CWH = "州市创新委员会" # 州市创新委员会
        
    # 项目评审组成员角色
    class ProjectGroupMemberRole(str,ExtendedEnum):
        LEADER = "组长"
        NORMAL = "组员"
    
    # 保存文件路径前缀
    FILE_PATH = "D:\\temp"
    
    # 临时文件路径
    TEMP_FILE_PATH = "D:\\temp\\temp"
    
    

settings = Settings()
# 日志处理器
log_handlers = []
# 日志级别 
log_level = logging.DEBUG
# 日志格式 log_formatter 
log_formatter = logging.Formatter(
    fmt="%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
)
# 初始化日志配置
def init_logger():
    global log_handlers
    if log_handlers.__len__() == 0:
        # 日志控制台输出流处理器
        stream_log_handler = logging.StreamHandler(sys.stdout)
        stream_log_handler.setLevel(log_level)
        stream_log_handler.setFormatter(log_formatter)
        log_handlers.append(stream_log_handler)
        # 日志文件输出流处理器
        file_log_handler = logging.handlers.TimedRotatingFileHandler(filename="ims.log", encoding='UTF-8',when="w6")
        file_log_handler.setLevel(log_level)
        file_log_handler.setFormatter(log_formatter)
        log_handlers.append(file_log_handler)

def get_logger(logging_name):
    # 获取logger对象,取名
    logger = logging.getLogger(logging_name)
    logger.setLevel(log_level)
    
    # 若日志处理器列表为空则初始化
    if log_handlers.__len__() == 0:
        init_logger()
    
    for handler in log_handlers:
        logger.addHandler(handler)
    
    return logger
